package na.learn.java.serialize;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

public class TestSerialization {

	protected static void serialize() {
		try {
			DummyInterface dummy = new Dummy();
			dummy.setNum(3333);
			FileOutputStream f_os = new FileOutputStream(
					"C:\\Workspace\\Learn4\\JavaThreads\\Dummy.txt");
			ObjectOutputStream o_os = new ObjectOutputStream(f_os);
			o_os.writeObject(dummy);
			System.out.println("Success");

		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public static void deserialize() {
		try {
			ObjectInputStream o_is = new ObjectInputStream(new FileInputStream(
					"C:\\Workspace\\Learn4\\JavaThreads\\Dummy.txt"));
			Object obj = o_is.readObject();
			if (obj instanceof DummyInterface) {
				System.out.println("Num:  " + ((DummyInterface) obj).getNum());

			}
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	public static void main(String[] args) {
		serialize();
		deserialize();
	}
}
